<script setup lang="ts">
/**
 * 系统设置-系统管理-角色管理-编辑角色
 */
import {onMounted, ref} from 'vue'
import {getPermissionListApi} from '@/assets/api/systemManagement/system/permission'
import {editRoleApi} from '@/assets/api/systemManagement/system/role'
import {ElMessage, type FormInstance} from 'element-plus'
import PermissionGroup from '@/components/base/PermissionGroup.vue'

const props = defineProps<{
  form: {
    roleName: string,
    roleType: string,
    roleDesc: string,
    status: number,
    remark: string,
    permissionIds: string
  },
  echoPermissions: number[] // 选中的权限
}>()

const emit = defineEmits(['save', 'close'])

// 获取所有权限
const permissionList = ref([])
const selectedPermissions = ref(props.echoPermissions)
const getPermissionList = () => {
  getPermissionListApi().then(res => {
    if (res.data.code === 200)
      permissionList.value = res.data.data
    else
      ElMessage.error({message: res.data.data, showClose: true})
  })
}

// 权限组回调
const permissionGroupCallBack = (value: number[]) => {
  selectedPermissions.value = value
}

// 表单验证
const formRef = ref()
const permissionValidator = (rule: any, value: string, callback: any) => {
  if (selectedPermissions.value.length === 0) {
    callback(new Error('请选择权限'))
  } else {
    callback()
  }
}
const rules = ref({
  roleName: [
    {required: true, message: '请输入角色名称', trigger: 'blur'}
  ],
  roleType: [
    {required: true, message: '请输入角色标识', trigger: 'blur'}
  ],
  roleDesc: [{required: true, message: '请输入角色标识', trigger: 'blur'}],
  selectedPermissions: [{required: true, validator: permissionValidator, trigger: 'blur'}
  ],
  status: [{required: true, message: '请选择状态', trigger: 'blur'}]
})

// 保存
const saveLoading = ref(false)
const save = async (formEl: FormInstance) => {
  if (!formEl) return
  await formEl.validate((valid: boolean) => {
    if (valid) {
      props.form.permissionIds = selectedPermissions.value.join(',')
      props.form.roleType = 'ROLE_' + props.form.roleType
      saveLoading.value = true
      editRoleApi(props.form).then(res => {
        saveLoading.value = false
        if (res.data.code === 200) {
          ElMessage.success({message: res.data.data, showClose: true})
          emit('save', true)
        } else {
          ElMessage.error({message: res.data.data, showClose: true})
        }
      })
    }
  })
}

onMounted(() => {
  const roleType = props.form.roleType
  if (roleType != undefined)
    props.form.roleType = roleType.replace('ROLE_', '')
  getPermissionList()
})
</script>

<template>
  <div class="form-div">
    <el-form v-loading="saveLoading" :model="form" label-width="80px" :rules="rules" ref="formRef">
      <el-form-item label="角色名称" prop="roleName">
        <el-input v-model="form.roleName" placeholder="请输入角色名称"></el-input>
      </el-form-item>
      <el-form-item label="角色标识" prop="roleType">
        <el-input v-model="form.roleType" placeholder="请输入角色标识">
          <template #prepend>ROLE_</template>
        </el-input>
      </el-form-item>
      <el-form-item label="角色描述" prop="roleDesc">
        <el-input v-model="form.roleDesc" placeholder="请输入角色描述"></el-input>
      </el-form-item>
      <el-form-item label="权限列表" prop="selectedPermissions">
        <permission-group :echo-permissions="selectedPermissions" :permission-list="permissionList"
                          @update:permissions="permissionGroupCallBack"></permission-group>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-radio-group v-model="form.status">
          <el-radio :value="1">启用</el-radio>
          <el-radio :value="0">停用</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="备注" prop="remark">
        <el-input v-model="form.remark" placeholder="请输入备注"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="save(formRef)">保存</el-button>
        <el-button @click="$emit('close')">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<style scoped>

</style>
